const express = require("express");
var router = express.Router();
const fe = require('fs-extra')
const path = require('path');
const directoryPath = './data';

/* 读取data目录下的文件 */
router.get("/", async (req, res) => {
  try {
    const files = await fe.readdir(directoryPath);
    const jsonFiles = files.filter(file => path.extname(file) === '.json');

    const jsonPromises = jsonFiles.map(async file => {
      const filePath = path.join(directoryPath, file);
      const data = await fe.readFile(filePath, 'utf8');
      return JSON.parse(data);
    });

    const json = await Promise.all(jsonPromises);
    res.send(json);
  } catch (error) {
    res.status(500).send('Internal Server Error');
  }
})

module.exports = router;
